package com.meng.baselibrary.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * Created by Administrator on 2017/5/4.
 */

public abstract class BaseActivity extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS );
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setMiuiStatusBarDarkMode(true);
        setContentView();
        initView();
        initData();
    }
    protected abstract void setContentView();
    protected abstract void initView();
    protected abstract void initData();
    protected  void startActivity(Class<?> clazz){
        Intent intent = new Intent(this,clazz);
        startActivity(intent);
    }
    protected <T extends View> T findViewByIdNoCast(int viewId){
        return (T) findViewById(viewId);
    }

    /**
     * 修改MIUI 状态栏的文字颜色为暗色或亮色
     * @param darkmode
     * @return
     */
    protected   boolean setMiuiStatusBarDarkMode(boolean darkmode) {
        Class<? extends Window> clazz = this.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(this.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 获取状态栏高度
     */
    public int getStatusHeight() {
        final Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int n = rect.top;
        if (n != 0) {
            return n;
        }
        try {
            final Class<?> forName = Class.forName("com.android.internal.R$dimen");
            n = getResources().getDimensionPixelSize(Integer.parseInt(forName.getField("status_bar_height").get(forName.newInstance()).toString()));
            return n;
        }catch (Exception ex) {
            return n;
        }
    }

    /**
     * 解决实现沉浸式状态栏出现的布局内容覆盖了状态栏问题
     */
    protected void initPaddingTob(){
        View contentView = getActivityView();
        if(contentView != null){
            contentView.setPadding(contentView.getPaddingLeft()
                    ,contentView.getPaddingTop()+getStatusHeight(),
                    contentView.getPaddingRight(),
                    contentView.getPaddingBottom());
            contentView.requestLayout();
        }
    }

    /**
     * 获取Activity的根布局(setContentView方法设置的)
     * @return
     */
    private View getActivityView(){
        ViewGroup contentGroup = (ViewGroup) findViewById(android.R.id.content);
        if(contentGroup.getChildCount() > 0){
            return contentGroup.getChildAt(0);
        }
        return null;
    }
}
